# +----------------------------------------------------------------------
# | ChatWork智能聊天办公系统
# +----------------------------------------------------------------------
# | 软件声明: 本系统并非自由软件,未经授权任何形式的商业使用均属非法。
# | 版权保护: 任何企业和个人不允许对程序代码以任何形式任何目的复制/分发。
# | 授权要求: 如有商业使用需求,请务必先与版权所有者取得联系并获得正式授权。
# +----------------------------------------------------------------------
# | Author: ChatWork Team <2474369941@qq.com>
# +----------------------------------------------------------------------
import time
from typing import List
from exception import AppException
from common.utils.times import TimeUtil
from common.utils.config import ConfigUtil
from common.models.market import RechargePackageModel
from apps.admin.schemas.market import recharge_schema as schema


class RechargeService:

    @classmethod
    async def lists(cls) -> List[schema.RechargePackageListVo]:
        """
        充值套餐详情

        Returns:
            List[schema.RechargePackageListVo]: 充值套餐列表Vo。

        Author:
            zero
        """
        lists = await RechargePackageModel.filter(is_delete=0).order_by("-sort", "-id").all()
        _lists = []
        for item in lists:
            _lists.append(schema.RechargePackageListVo(
                id=item.id,
                name=item.name,
                sell_price=item.sell_price,
                line_price=item.line_price,
                points_num=item.points_num,
                train_num=item.train_num,
                robot_num=item.robot_num,
                know_num=item.know_num,
                mcp_num=item.mcp_num,
                sort=item.sort,
                is_disable=item.is_disable,
                create_time=TimeUtil.timestamp_to_date(item.create_time),
                update_time=TimeUtil.timestamp_to_date(item.update_time),
            ))

        return _lists

    @classmethod
    async def detail(cls, id_: int) -> schema.RechargePackageDetailVo:
        """
        充值套餐详情

        Args:
            id_ (int): 套餐ID。

        Returns:
            schema.RechargePackageDetailVo: 充值套餐详情Vo。

        Author:
            zero
        """
        package = await RechargePackageModel.filter(id=id_, is_delete=0).get()
        return schema.RechargePackageDetailVo(
            id=package.id,
            name=package.name,
            tags=package.tags,
            sell_price=package.sell_price,
            line_price=package.line_price,
            points_num=package.points_num,
            train_num=package.train_num,
            robot_num=package.robot_num,
            know_num=package.know_num,
            mcp_num=package.mcp_num,
            sort=package.sort,
            is_disable=package.is_disable,
        )

    @classmethod
    async def add(cls, post: schema.RechargePackageAddIn):
        """
        充值套餐新增

        Args:
            post (schema.RechargePackageAddIn): 套餐新增参数。

        Author:
            zero
        """
        if post.sell_price <= 0:
            raise AppException("套餐价格必须大于0")
        if post.line_price < 0:
            raise AppException("原始价格不能少于0")
        if post.points_num < 0 and post.train_num < 0 and post.robot_num < 0 and post.know_num < 0:
            raise AppException("套餐内容中必须至少启用1个不能为0")

        await RechargePackageModel.create(
            name=post.name,
            tags=post.tags,
            sell_price=post.sell_price,
            line_price=post.line_price,
            points_num=post.points_num,
            train_num=post.train_num,
            robot_num=post.robot_num,
            know_num=post.know_num,
            mcp_num=post.mcp_num,
            sort=post.sort,
            is_disable=post.is_disable,
            create_time=int(time.time()),
            update_time=int(time.time())
        )

    @classmethod
    async def edit(cls, post: schema.RechargePackageEditIn):
        """
        充值套餐编辑

        Args:
            post (schema.RechargePackageEditIn): 套餐编辑参数。

        Author:
            zero
        """
        if post.line_price < 0:
            raise AppException("原始价格不能少于0")
        if post.sell_price <= 0:
            raise AppException("套餐价格必须大于0")
        if post.points_num < 0 and post.train_num < 0 and post.robot_num < 0 and post.know_num < 0:
            raise AppException("套餐内容中必须至少启用1个不能为0")

        await RechargePackageModel.filter(id=post.id, is_delete=0).get()
        await RechargePackageModel.filter(id=post.id).update(
            name=post.name,
            tags=post.tags,
            sell_price=post.sell_price,
            line_price=post.line_price,
            points_num=post.points_num,
            train_num=post.train_num,
            robot_num=post.robot_num,
            know_num=post.know_num,
            mcp_num=post.mcp_num,
            sort=post.sort,
            is_disable=post.is_disable,
            update_time=int(time.time())
        )

    @classmethod
    async def delete(cls, id_: int):
        """
        充值套餐删除

        Args:
            id_ (int): 套餐ID。

        Author:
            zero
        """
        await RechargePackageModel.filter(id=id_).get()
        await RechargePackageModel.filter(id=id_).update(
            is_delete=1,
            delete_time=int(time.time())
        )

    @classmethod
    async def get_config(cls) -> schema.RechargeConfigIn:
        """
        充值配置读取

        Returns:
            schema.RechargeConfigIn: 充值套餐配置Vo。

        Author:
            zero
        """
        status = await ConfigUtil.get("recharge", "status", "0")
        return schema.RechargeConfigIn(status=int(status))

    @classmethod
    async def set_config(cls, post: schema.RechargeConfigIn):
        """
        充值配置修改

        Args:
            post (schema.RechargeConfigIn): 配置参数。

        Author:
            zero
        """
        await ConfigUtil.set("recharge", "status", post.status)
